﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace System.Runtime.Caching
{
    internal static class SqlDependencyManager
    {
        private static readonly List<string> ConnectionStrings;
        private static readonly object LockObj = new object();

        static SqlDependencyManager()
        {
            ConnectionStrings = new List<string>();
            AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
            {
                foreach (var connectionString in ConnectionStrings)
                {
                    SqlDependency.Stop(connectionString);
                }
            };
        }

        internal static void AddConnectionString(string connectionString)
        {
            if (!ConnectionStrings.Contains(connectionString))
            {
                lock (LockObj)
                {
                    if (!ConnectionStrings.Contains(connectionString))
                    {
                        SqlDependency.Start(connectionString);
                        ConnectionStrings.Add(connectionString);
                    }
                }
            }
        }
    }
}
